package MyApp;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MyListAdapter2 extends RecyclerView.Adapter<MyListAdapter2.MyViewHolder> {

    private int resource; // Layout resource ID
    private Context context;
    private ArrayList<HashMap<String, String>> mData; // Data list

    public MyListAdapter2(Context context, int resource, ArrayList<HashMap<String, String>> data) {
        this.context = context;
        this.resource = resource;
        mData = data;
    }

    public void addAll(List<HashMap<String, String>> data) {
        mData.clear();
        mData.addAll(data);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MyListAdapter2.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(resource, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyListAdapter2.MyViewHolder holder, int position) {
        HashMap<String, String> map = mData.get(position); // Get data from mData
        holder.firstTextView.setText(map.get("firstText"));
        holder.secondTextView.setText(map.get("secondText"));
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView firstTextView;
        TextView secondTextView;

        public MyViewHolder(View itemView) {
            super(itemView);
            firstTextView = itemView.findViewById(R.id.firstTextView);
            secondTextView = itemView.findViewById(R.id.secondTextView);
        }
    }
}